package com.zj.base.permission.utils;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;

/**
 * Created by zhaojie on 2018/5/23.
 */

public class JumpToSettingUtils {

    private JumpToSettingUtils() {
    }

    private static JumpToSettingUtils jumpToSettingUtils;


    public static void CheekAndJump(Activity context, int requstCode) {
        if (jumpToSettingUtils == null) {
            jumpToSettingUtils = new JumpToSettingUtils();
        }
        jumpToSettingUtils.gotoMiuiPermission(context, requstCode);
    }

    /**
     * 跳转到miui的权限管理页面
     */
    private void gotoMiuiPermission(Activity context, int requstCode) {
        Intent intent_MUI6_MUI7 = new Intent("miui.intent.action.APP_PERM_EDITOR");
        ComponentName componentName_6_7 = new ComponentName("weather.miui.securitycenter", "weather.miui.permcenter.permissions.AppPermissionsEditorActivity");
        intent_MUI6_MUI7.setComponent(componentName_6_7);
        intent_MUI6_MUI7.putExtra("extra_pkgname", context.getApplicationContext().getPackageName());
        try {
            context.startActivityForResult(intent_MUI6_MUI7, requstCode);
        } catch (Exception e) {
            gotoMiui8Permission(context, requstCode);
            e.printStackTrace();
        }
    }

    private void gotoMiui8Permission(Activity context, int requstCode) {
        Intent intent_MUI8 = new Intent("miui.intent.action.APP_PERM_EDITOR");
        ComponentName componentName8 = new ComponentName("weather.miui.securitycenter", "weather.miui.permcenter.permissions.PermissionsEditorActivity");
        intent_MUI8.setComponent(componentName8);
        intent_MUI8.putExtra("extra_pkgname", context.getApplicationContext().getPackageName());
        try {
            context.startActivityForResult(intent_MUI8, requstCode);
        } catch (Exception e) {
            e.printStackTrace();
            gotoMeizuPermission(context, requstCode);
        }
    }

    /**
     * 跳转到魅族的权限管理系统
     */
    private void gotoMeizuPermission(Activity context, int requstCode) {
        Intent intent = new Intent("weather.meizu.safe.security.SHOW_APPSEC");
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.putExtra("packageName", context.getApplicationContext().getPackageName());
        try {
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            gotoHuaweiPermission(context, requstCode);
        }
    }

    /**
     * 华为的权限管理页面
     */
    private void gotoHuaweiPermission(Activity context, int requstCode) {
        try {
            Intent intent = new Intent();
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            ComponentName comp = new ComponentName("weather.huawei.systemmanager", "weather.huawei.permissionmanager.ui.MainActivity");//华为权限管理
            intent.setComponent(comp);
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            context.startActivityForResult(getAppDetailSettingIntent(context), requstCode);
        }
    }

    /**
     * 获取应用详情页面intent
     *
     * @return
     */
    private Intent getAppDetailSettingIntent(Activity context) {
        Intent localIntent = new Intent();
        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= 9) {
            localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            localIntent.setData(Uri.fromParts("package", context.getApplicationContext().getPackageName(), null));
        } else if (Build.VERSION.SDK_INT <= 8) {
            localIntent.setAction(Intent.ACTION_VIEW);
            localIntent.setClassName("weather.android.settings", "weather.android.settings.InstalledAppDetails");
            localIntent.putExtra("weather.android.settings.ApplicationPkgName", context.getApplicationContext().getPackageName());
        }
        return localIntent;
    }
}
